#pragma once

#include <iostream>
#include <string>
#include <unordered_map>
#include <functional>
#include "TcpServer.hpp"
#include "HttpProtocol.hpp"

using namespace TcpServerModule;
using http_hander_t = std::function<void (HttpRequest&, HttpResponse&)>; 
class HttpServer
{
public:
    HttpServer(int port)
        : _tsvr(std::make_unique<TcpServer>(port))
    {
    }
    void Register(std::string funcname, http_hander_t func)
    {
        _route[funcname] = func;
    }
    bool SafeCheck(const std::string& service)
    {
        auto iter = _route.find(service);
        return iter != _route.end();
    }
    void Start()
    {
        _tsvr->InitServer([this](SockPtr sockfd, InetAddr client)
                          { return this->HandlerHttpRequest(sockfd, client); });
        _tsvr->Loop();
    }

    // 处理http的入口
    bool HandlerHttpRequest(SockPtr sockfd, InetAddr client)
    {
        LOG(LogLevel::DEBUG) << "HttpServer: get a new client: " << sockfd->Fd() << " Addr info: " << client.Addr();
        std::string http_request;
        sockfd->Recv(&http_request);
        HttpRequest req;
        req.Deserialize(http_request);
        HttpResponse resp;
        // 请求被分为两类
        // 1.一般的静态资源
        // 2.携带参数的动态资源，需要进行交互式设置

        if (req.IsHasArgs())
        {
            // 处理动态资源
            std::string service = req.Path();
            if(SafeCheck(service))
                _route[req.Path()](req, resp);  // wwwroot/login?k=v&k=v.....
            else
                resp.Build(req); // 404页面
        }
        else
        {
            // 处理静态资源
            resp.Build(req);
        }
        std::string resp_str;
        resp.Serialize(&resp_str);
        sockfd->Send(resp_str); // http完成了对报文的编码

        // // std::cout << http_request; //字节流式的请求
        // // 读取请求，对请求进行分析处理 --》 文本处理
        // // 读取请求的完整性
        // // 完整的http反序列化，http response序列化
        // std::string status_line = "HTTP/1.1 200 OK" + Sep + BlankLine; // 状态行 + 空行
        // // 直接返回一个html网页
        // std::string body = "<!DOCTYPE html>"
        //                    "<html>"
        //                    "<head>"
        //                    "<meta charset=\"UTF-8\">"
        //                    "<title>郭家良爱上东北雨姐</title>"
        //                    "<style>"
        //                    "body {"
        //                    "    margin: 0;"
        //                    "    padding: 0;"
        //                    "    display: flex;"
        //                    "    justify-content: center;"
        //                    "    align-items: center;"
        //                    "    height: 100vh;"
        //                    "    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);"
        //                    "    font-family: 'STXingkai', 'KaiTi', cursive;"
        //                    "}"
        //                    ".calligraphy {"
        //                    "    font-size: 4rem;"
        //                    "    color: white;"
        //                    "    text-shadow: 3px 3px 8px rgba(0,0,0,0.3);"
        //                    "    text-align: center;"
        //                    "    letter-spacing: 8px;"
        //                    "}"
        //                    "</style>"
        //                    "</head>"
        //                    "<body>"
        //                    "<div class=\"calligraphy\">郭家良爱上东北雨姐</div>"
        //                    "</body>"
        //                    "</html>";
        // std::string response = status_line + body;
        // sockfd->Send(response);

        return true;
    }
    ~HttpServer() {}

private:
    std::unique_ptr<TcpServer> _tsvr;
    std::unordered_map<std::string, http_hander_t> _route; //功能路由
};